package com.googlecode.slm.service;

import com.googlecode.slm.daomapper.TreeMapper;
import com.googlecode.slm.domain.dao.Tree;
import com.googlecode.slm.domain.dao.TreeGard;
import com.googlecode.slm.domain.web.TreeBasicAttribute;
import com.googlecode.slm.domain.web.TreeNode;
import com.googlecode.slm.domain.web.TreeNodeAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: liguangjian
 * Date: 11-10-8
 * Time: 下午7:26
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TreeService {

    @Autowired
    @Qualifier(value = "treeMapper")
    private TreeMapper treeMapper;


    public TreeMapper getTreeMapper() {
        return treeMapper;
    }

    public void setTreeMapper(TreeMapper treeMapper) {
        this.treeMapper = treeMapper;
    }

    public TreeBasicAttribute getTreeNode(int id){
    	Tree tree = getTreeMapper().getIDTree(id);
    	return setTreeNode(tree);
    }
    
    private TreeBasicAttribute setTreeNode(Tree tree){
    	TreeBasicAttribute node = new TreeBasicAttribute(tree.getId(), 
    			tree.getPid(), 
    			tree.getName(), 
    			tree.getLeafage(), 
    			tree.getUrl(), 
    			tree.getClassname(), 
    			tree.getDescription(), 
    			tree.getAvailability());
    	return node;
    }
    
    public List<TreeNode> getTreeList(int pid){
        List<com.googlecode.slm.domain.dao.Tree> listTree = getTreeMapper().getPidListTree(pid);
        return setTreeNodeList(listTree);
    }
    
    /**
     * @param pid
     * @param gid 级别
     * @return
     */
    public List<TreeNode> getUserTreeList(int pid, int gid){
    	TreeGard gard = new TreeGard(gid);
    	gard.setPid(pid);
        List<com.googlecode.slm.domain.dao.Tree> listTree = getTreeMapper().getUserListTree(gard);
        return setTreeNodeList(listTree);
    }
    
    /**
     * @param pid
     * @param gid 级别
     * @return
     */
    public List<TreeNode> getUserGardTreeList(int pid, int gid){
    	TreeGard gard = new TreeGard(gid);
    	gard.setPid(pid);
        List<com.googlecode.slm.domain.dao.TreeGard> listTree = getTreeMapper().getListGardTree(gard);
        return setTreeNodeGardList(listTree);
    }
    
    private List<TreeNode> setTreeNodeGardList(List<com.googlecode.slm.domain.dao.TreeGard> listTree){
    	List<TreeNode> list = new ArrayList<TreeNode>();
    	TreeNode node = null;
    	for (com.googlecode.slm.domain.dao.TreeGard tree : listTree){
    		node = new TreeNode(tree.getId(), tree.getPid(), tree.getName());
            node.setAttributes(new TreeNodeAttributes(tree.getPid(), tree.getUrl(), tree.getLeafage()));
            if (tree.getLeafage()==1){
            	node.setState("open");
            } else {
                node.setState("closed");
            } 
            if (tree.getGid()==0){
            	node.setChecked(false);
            } else {
            	node.setChecked(true);
            }
            list.add(node);
    	}
    	return list;
    }
    
    private List<TreeNode> setTreeNodeList(List<com.googlecode.slm.domain.dao.Tree> listTree){
    	List<TreeNode> list = new ArrayList<TreeNode>();
    	TreeNode node = null;
    	for (com.googlecode.slm.domain.dao.Tree tree : listTree){
    		node = new TreeNode(tree.getId(), tree.getPid(), tree.getName());
            node.setAttributes(new TreeNodeAttributes(tree.getPid(), tree.getUrl(), tree.getLeafage()));
            if (tree.getLeafage()==1){
               node.setState("open");
            } else {
                node.setState("closed");
            }
            list.add(node);
    	}
    	return list;
    }
    
    @Transactional
    public boolean saveGardTree(int[] fid, int gardid){
    	List<TreeGard> list = new ArrayList<TreeGard>();
    	TreeGard treeGard = null;
    	getTreeMapper().deleteGardTree(gardid);
    	for (int tem : fid){
    		treeGard = new TreeGard();
    		treeGard.setId(tem);
    		treeGard.setGid(gardid);
    		list.add(treeGard);
    		getTreeMapper().insertGardTree(treeGard);
    	}
    	//getTreeMapper().deleteGardTree(gardid);
    	//getTreeMapper().insertListGardTree(list);
    	return true;
    }
    
    @Transactional
    public List<TreeNode> saveTree(TreeBasicAttribute tree){
    	Tree node = new Tree(tree.getId(), 
    			tree.getPid(), 
    			tree.getName(), 
    			tree.getLeafage(), 
    			tree.getUrl(), 
    			tree.getClassname(), 
    			tree.getDescription(), 
    			tree.getAvailability());
    	
    	getTreeMapper().insetTree(node);
    	int id = getTreeMapper().getinsetTreeId();
    	if (id>0){
	    	List<TreeNode> list = new ArrayList<TreeNode>();
	    	TreeNode treeNode = new TreeNode(id, tree.getPid(), tree.getName());
	    	if (tree.getLeafage()==1){
	    		treeNode.setState("open");
	         } else {
	        	 treeNode.setState("closed");
	         }
	    	treeNode.setAttributes(new TreeNodeAttributes(tree.getPid(), tree.getUrl(), tree.getLeafage()));
	    	list.add(treeNode);
	    	return list;
    	}
    	return null;
    }
    
    @Transactional
    public int deleteTree(int id){
    	return getTreeMapper().deleteTree(id);
    }
}
